package com.qkl.xiatang.app.utils.appenum;

import com.qkl.xiatang.app.dto.user.UserOutDTO;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

@Getter
public enum MessageTypeSubEnum {
    //系统消息
    ADMIN_STOCK(MessageTypeSub.ADMIN_STOCK,"库存不足"),
    ADMIN_SENSOR(MessageTypeSub.ADMIN_SENSOR,"传感器数值异常"),
    ADMIN_SENSOR_OFF_LINE(5102,"设备已离线"),
    FEED_NOT_FEED(MessageTypeSub.FEED_NOT_FEED,"饲料未投放"),
    CHECK_UNCHECK(MessageTypeSub.CHECK_UNCHECK,"未查料台"),
    ZOO_NOT_FEED(MessageTypeSub.ZOO_NOT_FEED,"动保未投放"),

    //称饲料
    WEIGH_FEED_FINISH(MessageTypeSub.WEIGH_FEED_FINISH,"饲料配发,注意称料"),
    WEIGH_FEED_CANCEL(MessageTypeSub.WEIGH_FEED_CANCEL,"取消配料"),
    WEIGH_FEED_NO_ALLOC(MessageTypeSub.WEIGH_FEED_NO_ALLOC,"未配料塘号"),

    //喂饲料
    FEED_WEIGH_FINISH(MessageTypeSub.FEED_WEIGH_FINISH,"饲料称完"),
    FEED_START(MessageTypeSub.FEED_START,"饲料投放"),
    FEED_END(MessageTypeSub.FEED_END,"饲料完全投放"),
    FEED_CANCEL(MessageTypeSub.FEED_CANCEL,"饲料取消投放"),

    //查料台
    CHECK_FEED_START(MessageTypeSub.CHECK_FEED_START,"饲料投放，注意查料台"),
    CHECK_START(MessageTypeSub.CHECK_START,"开始查料台"),
    CHECK_FINISH(MessageTypeSub.CHECK_FINISH,"查料台完成"),

    //称动保
    WEIGH_ZOO_ALLOC(MessageTypeSub.WEIGH_ZOO_ALLOC,"动保配发,注意称动保"),
    WEIGH_ZOO_CANCEL(MessageTypeSub.WEIGH_ZOO_CANCEL,"取消动保"),

    //打动保
    ZOO_WEIGH_FINISH(MessageTypeSub.ZOO_WEIGH_FINISH,"动保称完"),
    ZOO_FINISH(MessageTypeSub.ZOO_FINISH,"动保完全投放"),
    ZOO_CANCEL(MessageTypeSub.ZOO_CANCEL,"动保取消投放"),
    ZOO_START(MessageTypeSub.ZOO_START,"动保投放"),
    ;

    private MessageTypeSubEnum(Integer subType,String name){
        this.name = name;
        this.subType = subType;
        EnumUtil.typeMap.put(subType,this);
    }

    private Integer subType;

    private String name;

    /**
     * 称料
     * @return
     */
    public static Map<Integer,MessageTypeSubEnum> weighSubType(){
        return putSubTypeList(WEIGH_FEED_FINISH,WEIGH_FEED_CANCEL,WEIGH_FEED_NO_ALLOC,WEIGH_ZOO_ALLOC,WEIGH_ZOO_CANCEL);
    }
    /**
     * 喂饲料
     * @return
     */
    public static Map<Integer,MessageTypeSubEnum> feedSubType(){
        return putSubTypeList(FEED_WEIGH_FINISH,FEED_START,FEED_END,FEED_CANCEL,ZOO_WEIGH_FINISH,ZOO_FINISH,ZOO_CANCEL,ZOO_START);
    }

    /**
     * 查料台
     * @return
     */
    public static Map<Integer,MessageTypeSubEnum> checkSubType(){
        return putSubTypeList(CHECK_FEED_START,CHECK_START,CHECK_FINISH);
    }

    public static Map<Integer,MessageTypeSubEnum> userSubType(UserOutDTO userAppInfo){
        if(null == userAppInfo || userAppInfo.isAlloc()  || userAppInfo.isAdmin() || userAppInfo.isHolder() || userAppInfo.isObserve()){
            Map<Integer,MessageTypeSubEnum> allSubType = allSubType();
            //系统消息不能屏蔽
            allSubType.remove(MessageTypeSubEnum.ADMIN_STOCK.getSubType());
            allSubType.remove(MessageTypeSubEnum.ADMIN_SENSOR.getSubType());
            allSubType.remove(MessageTypeSubEnum.ADMIN_SENSOR_OFF_LINE.getSubType());
            allSubType.remove(MessageTypeSubEnum.FEED_NOT_FEED.getSubType());
            allSubType.remove(MessageTypeSubEnum.CHECK_UNCHECK.getSubType());
            allSubType.remove(MessageTypeSubEnum.ZOO_NOT_FEED.getSubType());
            return allSubType;
        } else {
            Map<Integer,MessageTypeSubEnum> subEnumMap = new HashMap<>();
            if(userAppInfo.isWeigh()){
                subEnumMap.putAll(weighSubType());
            }else if(userAppInfo.isFeed()){
                subEnumMap.putAll(feedSubType());
            }else if(userAppInfo.isCheck()){
                subEnumMap.putAll(checkSubType());
            }
            return subEnumMap;
        }
    }


    public static Map<Integer,MessageTypeSubEnum> allSubType(){
        Map<Integer,MessageTypeSubEnum> map = new HashMap<>();
        map.putAll(EnumUtil.typeMap);
        return map;
    }

    public static MessageTypeSubEnum getSubType(Integer subType){
        return EnumUtil.typeMap.get(subType);
    }

    private static Map<Integer,MessageTypeSubEnum> putSubTypeList(MessageTypeSubEnum...subEnums){
        Map<Integer,MessageTypeSubEnum> map = new HashMap<>();
        for(MessageTypeSubEnum subEnum : subEnums){
            map.put(subEnum.getSubType(),subEnum);
        }
        return map;
    }

    private static  class EnumUtil{
        public static Map<Integer,MessageTypeSubEnum> typeMap = new HashMap<>();
    }

}
